//
//  NotesViewController.m
//  chanyou
//
//  Created by wanli on 15/1/2.
//  Copyright (c) 2015年 wanli. All rights reserved.
//

#import "NotesViewController.h"
#import "NotesTableViewCell.h"
#import "ConnectModel.h"
#import "UIImageView+WebCache.h"
#import "TripsDetailViewController.h"
#import "SDImageCache.h"
#import "SideTableViewCell.h"
@interface NotesViewController ()

@end

@implementation NotesViewController


- (void)dealloc
{
    [_destination_id release];
    [_tableArray release];
    [_tableView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatTableView];
    [self startConnect];
    
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-back.png"] style:UIBarButtonItemStyleDone target:self action:@selector(backAction)]autorelease];
    // Do any additional setup after loading the view.
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 45, 100)];
    label.textColor = [UIColor colorWithRed:255 / 255.0 green:69 / 255.0 blue:0 alpha:1];
    label.font = [UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:20];
    label.text = @"游记";
    self.navigationItem.titleView = label;
    [label release];

}

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
    
    //    [[SDImageCache sharedImageCache] clearMemory];
}

- (void)creatTableView{
    self.tableView = [[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain] autorelease];
    [_tableView registerClass:[NotesTableViewCell class] forCellReuseIdentifier:@"notesCell"];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];

}
- (void)startConnect{
    
    NSString *path = [NSString stringWithFormat:@"%@%@.json?page=1", @"http://chanyouji.com/api/destinations/trips/", _destination_id];
    __block NotesViewController *notes = self;
    [ConnectModel connectModelWithUrl:path parmaters:nil finished:^(NSData *data) {
        if (data == nil) {
            return ;
        }
        notes.tableArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        [notes.tableView reloadData];
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_tableArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NotesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"notesCell"];
    NSDictionary *dic = [_tableArray objectAtIndex:indexPath.row];
    NSString *imagePath = [dic objectForKey:@"front_cover_photo_url"];
//    [cell.image_urlView imageWithUrl:imagePath placeHolder:nil];
        [cell.image_urlView sd_setImageWithURL:[NSURL URLWithString:imagePath] placeholderImage:[UIImage imageNamed: @"placeHold.png"]];
    
    cell.name.text = [dic objectForKey:@"name"];
    cell.dayAndCount.text = [NSString stringWithFormat:@"%@ / %@天, %@图", [dic objectForKey:@"start_date"], [dic objectForKey:@"days"],[dic objectForKey:@"photos_count"]];
    NSDictionary *userDic = [dic objectForKey:@"user"];
    NSString *userPath = [userDic objectForKey:@"image"];
    
        [cell.userImageView sd_setImageWithURL:[NSURL URLWithString:userPath] placeholderImage:[UIImage imageNamed: @"placeHold.png"]];
    
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TripsDetailViewController *trip = [[TripsDetailViewController alloc] init];
    
    NSDictionary *dic = [_tableArray objectAtIndex:indexPath.row];
    NSInteger num = [[dic objectForKey:@"id"] intValue];
    trip.IdNum = num;
    [self.navigationController pushViewController:trip animated:YES];
        [[SDImageCache sharedImageCache] clearMemory];
    [trip release];
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.view.frame.size.height / 2.8;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
